package photorganizer.common.context;

import photorganizer.common.bean.Interceptor;
import photorganizer.common.bean.Request;
import photorganizer.common.logging.LoggingHome;

public class ContextInterceptor implements Interceptor {

	public Object invoke(Request request) throws Throwable {
		if (ContextHome.getContext() == null) {
			ContextHome.setContext(new SimpleContext());
		}

		try {
			return request.invoke();
		}
		finally {
			try {
				ContextHome.close();
			}
			catch (Throwable throwable) {
				LoggingHome.warning("Failed to close context", throwable);
			}
		}
	}

}
